<?php
/**
 * 更新food_status_content和food_status_tag 根据food_tag
 * 
 * @author yangchao
 * @since 2012-03-19
 */


if (!file_exists("../../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../../SINASRV_CONFIG", true); $_SERVER = array_merge($_SERVER, $array_config); 
}

define("CRON_NAME","cron_get_food_status_by_at_uids");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;
//15分钟执行一次 每次所有的uid,这里的uid不会很多
define("PCOUNT",100);//每次处理多少uid
define("API_PCOUNT",200);//每次抓取200个@微博
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblogv2/mod_get_mblogv2_from_api.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/foodv2/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/foodv2/crontab/inc.php");


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		$db_cron_at_uid = new db_cron_at_uid();
		
		//标记cron已经开始
		$res = $this->replace_into_data(0);	
		
		//获取uid的总数
		$count = $db_cron_at_uid->cron_get_at_uid_count();
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		//标记处理完全后退出
		$res = $this->replace_into_data(1);	
		
	}	
	
	function get_list_by_page($p)
	{
		$db_cron_at_uid = new db_cron_at_uid();
		
		//分批取列表
		$list = $db_cron_at_uid->cron_get_at_uid_list($p,PCOUNT);
		
	
		foreach($list as $value)
		{
			$uid = $value["uid"];
			$site_id = $value["site_id"];
			$start_id = $value["start_id"];
			if($uid)//处理抓取分页逻辑
			{
				$this->get_at_list_by_uid($uid,$start_id,1,$site_id);
			}
			
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_at_list_by_uid($uid,$since_id,$p,$site_id)
	{
		global $mid_have_get;
		$db_cron_at_uid = new db_cron_at_uid();
		$mblog_from_api = new mblog_from_api();
		$base_search = new base_search();
		$db_food_tag = new db_food_tag();
		$db_web_rstrt_cname = new db_web_rstrt_cname();
		$db_web_rstrt= new db_web_rstrt();
		$db_food_status = new db_food_status();
		$db_food_status_tag = new db_food_status_tag();
		
		if($p == 1 && $since_id==0)//当为第一页时并且是第一次抓取，检查是否有多页需要抓取
		{	
			$mblog_list = $mblog_from_api->api_mentions_list($uid,$since_id,"",$p,1,$filter_by_author=0,$filter_by_source=0,$filter_by_type=1);
			$total_page = ceil($mblog_list["data"]["count"]/API_PCOUNT);
			$total_page = $total_page > 20 ? 20 : $total_page;
			echo "topic_page_count:{$total_page}<br>\n";
			
			//这里设置了一个倒序取数据，先取时间早的微博。防止cron被kill的情况数据丢失
			for($i = $total_page ; $i >= 2;$i--)
			{
				$this->get_at_list_by_uid($uid,$since_id,$i,$site_id);
			}
			
		}
		
		$mod_food_status_content = new mod_food_status_content();
		//构造sql,一次性插入
		$sql_status = " insert ignore into food_status (status_id,mid,create_uid,status,update_time,last_update_time,update_ip,site_id,res_id,is_pic,is_rstrt) values ";
		$sql_status_array = array();
		
		$sql_status_tag = "insert ignore into food_status_tag ( status_id,tag,tag_hash,create_uid,create_nickname,create_time,last_update_time,status,create_ip,site_id,is_pic,is_rstrt) values ";
		$sql_status_tag_array = array();
		
		$uids_array = array();
		
		if($uid)
		{
			//根据话题抓取
			
			$mblog_list = $mblog_from_api->api_mentions_list($uid,$since_id,"",$p,API_PCOUNT,$filter_by_author=0,$filter_by_source=0,$filter_by_type=1);
		
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";	
			
			
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
					if(!$value1["user"]["id"])
					{
						//微薄已经被删除的情况
						continue;
					}
					//跟开始id一样，跳过
					if($value1["id"] <=  $since_id)
					{
						continue;
					}
					//获取数据后需要更新start_id
					if($value1["id"] > $new_start_id )			// WARRNG BY DONGPING
					{
						$new_start_id = $value1["idstr"];
					}
					
					if(strpos($value1["text"],"活动") !== false)
					{
						continue;
					}
					
					if(strpos($value1["text"],"话题") !== false)
					{
						continue;
					}
					
					if(strpos($value1["text"],"分享至") !== false)
					{
						continue;
					}
					if(strpos($value1["text"],"加入") !== false)
					{
						continue;
					}
					
					if(strpos($value1["soucre"],"美食推") !== false)
					{
						continue;
					}
					
					
					$u_time = strtotime($value1["created_at"]);
					$create_time = date("Y-m-d H:i:s",$u_time);
					
					
					//获取微博坐标信息
					$coordinates = "";
					if($value1["geo"]["coordinates"])
					{
						$coordinates = $value1["geo"]["coordinates"][1].",".$value1["geo"]["coordinates"][0];
					}
					//获取坐标对应地址
					if($coordinates)
					{
						 $address_info = $mblog_from_api->api_geo_to_address($coordinates);
						if($address_info["data"]["result"])
						{
							$location = $address_info["data"]["result"][0]["province_name"]." ".$address_info["data"]["result"][0]["city_name"]." ".$address_info["data"]["result"][0]["address"];
						} 
					}
					
					$split_word = $base_search->get_mblog_word_cut($value1["text"]);
					$last_status_id= $mod_food_status_content->insert_data_and_size($value1["user"]["id"],$value1["user"]["screen_name"],$value1["text"],$value1["original_pic"],0,$value1["source"],$create_time,$value1["mid"],$coordinates,$location,$split_word,true);
					$last_status_id = $last_status_id["status_id"];
					
					if($last_status_id)
					{
						$tag_array = array();
						$split_word = explode(" ",$split_word);
						foreach($split_word as $key => $value)
						{
							if(strlen($value) >3)
							{
								$tag_array[] = "'{$value}'";
							}
						}
						
						$tag_array = array_unique($tag_array);
						//先判断是否有一样的别名
						$list = $db_web_rstrt_cname->cron_get_res_id_by_cnames($tag_array,$site_id);
						if(!$list)
						{
							//判断是否有匹配的餐厅名
							$list = $db_web_rstrt->cron_get_res_id_by_res_names($tag_array,$site_id);
						}
						
						if(!$list)//如果没有匹配到餐厅名,取food_tag匹配
						{
							$tag_id_list = $db_food_tag->cron_get_food_tag_id_by_tag_names($tag_array,$site_id);
							
							if($mid_have_get)
							{
								//更新food_tag,如果已经有了，
								$res = $db_food_status->cron_mod_update_food_status_by_status_id($last_status_id,"",$site_id);
							}
							
							if(!$res)
							{
								$sql_status_array[] = "({$last_status_id}, {$value1["mid"]} , {$value1["user"]["id"]},  1 , '{$create_time}','{$create_time}',0,$site_id,0,1,0)";
							
							}
							
							if($mid_have_get)//food_status_content已经有数据
							{
								//更新 food_status_tag
								$res1 =$db_food_status_tag->cron_mod_update_food_status_by_status_id($last_status_id,"",$site_id);
							}
								
							foreach($tag_id_list as $tag_value)
							{
								
								if(!$res1)
								{
									$tag_hash = php_patch::php_crc32($tag_value["tag_name"]);
									$sql_status_tag_array[] = "({$last_status_id}, '{$tag_value["tag_name"]}',$tag_hash,{$value1["user"]["id"]} , '{$value1["user"]["screen_name"]}','{$create_time}',$u_time,1,0 ,$site_id,1,0)";
								}
								
							}
							
						}
						else
						{	//如果匹配到餐厅名，加入餐厅关联,只匹配一个餐厅
							$sql_status_array[] = "({$last_status_id}, {$value1["mid"]} , {$value1["user"]["id"]},  1 , '{$create_time}','{$create_time}',0,$site_id,{$list[0]["res_id"]},1,1)";
							
						}
						
						$uids_array[] = $value1["user"]["id"];
						
					}
					
					if(count($sql_status_tag_array) > 100 || count($sql_status_array) > 100)
					{
						if($sql_status_tag_array)
						{
							$sql  = $sql_status_tag. join(",",$sql_status_tag_array);
							$db_food_status_tag->query($sql);
							unset($sql_status_tag_array);
						}
						
						
						
						if($sql_status_array)
						{
							$sql  = $sql_status. join(",",$sql_status_array);
							$db_food_status_tag->query($sql);
							unset($sql_status_array);
						}
							
						
					}
					
					
				
				}
		
			}
				
		}
		if($sql_status_tag_array)
		{
			$sql  = $sql_status_tag. join(",",$sql_status_tag_array);
			$db_cron_at_uid->query($sql);
		}
		
		
		if($sql_status_array)
		{
			$sql  = $sql_status. join(",",$sql_status_array);
			$db_cron_at_uid->query($sql);
		}
		
		if($new_start_id)
		{
			//每次分页结束就更新start_id,较少数据丢失可能性
			$db_cron_at_uid->cron_update_start_id($uid,$new_start_id);
			
		}
		
		
		$uids_array = array_unique($uids_array);
		//更新用户分享数
		$mod_food_user = new mod_food_user();
		foreach($uids_array as $value)
		{
			$mod_food_user->update_food_user_status_count($value);
		}
		
			
		
	}
			
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
